// 入口文件

import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import VueResource from 'vue-resource'
Vue.use(VueResource)
Vue.http.options.root = 'http://vue.studyit.io/'
Vue.http.options.emulateJSON = true
import VuePreview from 'vue-preview'
Vue.use(VuePreview)
import Vuex from 'vuex'
Vue.use(Vuex)
//从本地中读取购物车详情
var car = JSON.parse(localStorage.getItem('car') || '[]')
const store = new Vuex.Store({
    state: {
        car: car //购物车详情
    },
    mutations: {
        addToCar(state, goodinfo) { //添加到购物车
            var flag = false

            state.car.some(item => {
                if(item.id === goodinfo.id){
                    item.count += parseInt(goodinfo.count)
                    flag = true
                    return true
                }
            })

            if(!flag){
                state.car.push(goodinfo)
            }
            //将购物车信息存储在本地
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updateCarCount(state,goodinfo) {//更新购物车数量
            state.car.some(item => {
                if(item.id === goodinfo.id){
                    item.count = parseInt(goodinfo.count)
                    return true
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        removeFromCar(state, id) { //删除购物车内容
            state.car.some( (item, i) => {
                if (item.id === id) {
                    state.car.splice(i, 1)
                    return true
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        clearAllCar(state) {//清空购物车
            localStorage.setItem('car', '[]')
        },
        updateSelected(state, obj){//更新购物车选中项
            state.car.some(item => {
                if(item.id === obj.id) {
                    item.selected = obj.val
                    return true
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car))
        }
    },
    getters: {
        getAllCount(state) { //获取购物车总数量
            var allCount = 0
            state.car.forEach( item => {
                allCount += item.count
            })
            return allCount
        },
        getItemCount(state) { //获取购物车每个商品的数量
            var o = {}
            state.car.forEach( item => {
                o[item.id] = item.count
            })
            return o
        },
        getItemSelected(state) { //获取购物车选中信息
            var o = {}
            state.car.forEach(item => {
                o[item.id] = item.selected
            })
            return o
        },
        getAllPrice(state) {//获取总价
            var o = {
                count: 0,   //数量
                amount: 0   //总价
            }
            state.car.forEach(item => {
                if(item.selected){
                    o.count += item.count
                    o.amount += (item.count*item.price)
                }
            })
            return o
        }
    }
})

import './lib/mui/css/mui.css'
import './lib/mui/css/icons-extra.css'
// import { Header, Swipe, SwipeItem, Button, Lazyload} from 'mint-ui'
// Vue.component(Header.name, Header)
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem)
// Vue.component(Button.name, Button)
// Vue.use(Lazyload)
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUI)

import moment from 'moment'
Vue.filter('dateFormat', function (dataStr, pattern='YYYY-MM-DD hh:mm:ss') {
    return moment(dataStr).format(pattern)
})

import app from './App.vue'
import router from './router.js'

var vm = new Vue({
    el:'#app',
    render: c=>c(app),
    router,
    store
})